package com.badshun.function;

import android.app.*;
import android.os.*;
import android.widget.*;
import android.view.View.*;
import android.view.*;
import android.widget.AdapterView.*;
import android.content.*;

//
//                            _ooOoo_
//                           o8888888o
//                           88" . "88
//                           (| -_- |)
//                           O\  =  /O
//                        ____/`---'\____
//                      .'  \\|     |//  `.
//                     /  \\|||  :  |||//  \
//                    /  _||||| -:- |||||-  \
//                    |   | \\\  -  /// |   |
//                    | \_|  ''\---/''  |   |
//                    \  .-\__  `-`  ___/-. /
//                  ___`. .'  /--.--\  `. . __
//               ."" '<  `.___\_<|>_/___.'  >'"".
//              | | :  `- \`.;`\ _ /`;.`/ - ` : | |
//              \  \ `-.   \_ __\ /__ _/   .-` /  /
//         ======`-.____`-.___\_____/___.-`____.-'======
//                            `=---='
//        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//                      佛祖保佑，代码无Bug！

public class MainActivity extends Activity 
{
	private int index;
	
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
		
		Button selectFunction = (Button) findViewById(R.id.select_function);
		
		selectFunction.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View p1) {
				final String[] functions = new String[]{"y = kx","y = kx + b","y = ax² + bx + c","y = a(x - h)² + b"};
				
				AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
				builder.setTitle("选择函数");
				
				ListView lv = new ListView(MainActivity.this);
				lv.setAdapter(new BadAdapter(functions));
				
				builder.setView(lv);
				final AlertDialog dialog = builder.show();
				
				lv.setOnItemClickListener(new OnItemClickListener() {
					@Override
					public void onItemClick(AdapterView<?> p1, View p2, int p3, long p4) {
						LinearLayout functionNameLayout = (LinearLayout) findViewById(R.id.function_name_layout);
						
						RelativeLayout rl1 = (RelativeLayout) findViewById(R.id.rl1);
						RelativeLayout rl2 = (RelativeLayout) findViewById(R.id.rl2);
						RelativeLayout rl3 = (RelativeLayout) findViewById(R.id.rl3);
						
						TextView functionName = (TextView) findViewById(R.id.function_name);
						TextView label1 = (TextView) findViewById(R.id.label1);
						TextView label2 = (TextView) findViewById(R.id.label2);
						TextView label3 = (TextView) findViewById(R.id.label3);
						
						Button begin = (Button) findViewById(R.id.begin);
						
						functionNameLayout.setVisibility(View.VISIBLE);
						
						functionName.setText(functions[p3]);
						
						dialog.dismiss();
						
						index = p3;
						
						if(p3 == 0) {
							rl1.setVisibility(View.GONE);
							rl2.setVisibility(View.GONE);
							rl3.setVisibility(View.GONE);
							begin.setVisibility(View.GONE);
							
							rl1.setVisibility(View.VISIBLE);
							begin.setVisibility(View.VISIBLE);
							
							label1.setText("k");
						} else if(p3 == 1) {
							rl1.setVisibility(View.GONE);
							rl2.setVisibility(View.GONE);
							rl3.setVisibility(View.GONE);
							begin.setVisibility(View.GONE);
							
							rl1.setVisibility(View.VISIBLE);
							rl2.setVisibility(View.VISIBLE);
							begin.setVisibility(View.VISIBLE);
							
							label1.setText("k");
							label2.setText("b");
						} else if(p3 == 2) {
							rl1.setVisibility(View.GONE);
							rl2.setVisibility(View.GONE);
							rl3.setVisibility(View.GONE);
							begin.setVisibility(View.GONE);

							rl1.setVisibility(View.VISIBLE);
							rl2.setVisibility(View.VISIBLE);
							rl3.setVisibility(View.VISIBLE);
							begin.setVisibility(View.VISIBLE);

							label1.setText("a");
							label2.setText("b");
							label3.setText("c");
						} else if(p3 == 3) {
							rl1.setVisibility(View.GONE);
							rl2.setVisibility(View.GONE);
							rl3.setVisibility(View.GONE);
							begin.setVisibility(View.GONE);

							rl1.setVisibility(View.VISIBLE);
							rl2.setVisibility(View.VISIBLE);
							rl3.setVisibility(View.VISIBLE);
							begin.setVisibility(View.VISIBLE);

							label1.setText("a");
							label2.setText("h");
							label3.setText("b");
						}
					}
				});
			}
		});
		
		Button begin = (Button) findViewById(R.id.begin);
		
		begin.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View p1) {
				EditText et1 = (EditText) findViewById(R.id.et1);
				EditText et2 = (EditText) findViewById(R.id.et2);
				EditText et3 = (EditText) findViewById(R.id.et3);
				
				Intent intent = new Intent(MainActivity.this,GDXFunctionActivity.class);
				
				Bundle bundle = new Bundle();
				bundle.putInt("TYPE",index);
				
				switch(index) {
					case 0:
						if(et1.getText().toString().trim().equals("")) {
							Toast.makeText(MainActivity.this,"请将数据填写完整",Toast.LENGTH_SHORT).show();
						} else {
							bundle.putFloat("k",new Float(et1.getText().toString().trim()));
						}
							
						break;
					case 1:
						if(et1.getText().toString().trim().equals("") || et2.getText().toString().trim().equals("")) {
							Toast.makeText(MainActivity.this,"请将数据填写完整",Toast.LENGTH_SHORT).show();
						} else {
							bundle.putFloat("k",new Float(et1.getText().toString().trim()));
							bundle.putFloat("b",new Float(et2.getText().toString().trim()));
						}
						
						break;
					case 2:
						if(et1.getText().toString().trim().equals("") || et2.getText().toString().trim().equals("") || et3.getText().toString().trim().equals("")) {
							Toast.makeText(MainActivity.this,"请将数据填写完整",Toast.LENGTH_SHORT).show();
						} else {
							bundle.putFloat("a",new Float(et1.getText().toString().trim()));
							bundle.putFloat("b",new Float(et2.getText().toString().trim()));
							bundle.putFloat("c",new Float(et3.getText().toString().trim()));
						}
						
						break;
					case 3:
						if(et1.getText().toString().trim().equals("") || et2.getText().toString().trim().equals("") || et3.getText().toString().trim().equals("")) {
							Toast.makeText(MainActivity.this,"请将数据填写完整",Toast.LENGTH_SHORT).show();
						} else {
							bundle.putFloat("a",new Float(et1.getText().toString().trim()));
							bundle.putFloat("h",new Float(et2.getText().toString().trim()));
							bundle.putFloat("b",new Float(et3.getText().toString().trim()));
						}
						
						break;
				}
				
				intent.putExtras(bundle);
				
				startActivity(intent);
			}
		});
    }
	
	private class BadAdapter extends BaseAdapter
	{
		private String[] datas;
		
		public BadAdapter(String[] datas) {
			this.datas = datas;
		}
		
		@Override
		public int getCount()
		{
			return datas.length;
		}

		@Override
		public Object getItem(int p1)
		{
			return null;
		}

		@Override
		public long getItemId(int p1)
		{
			return 0;
		}

		@Override
		public View getView(int p1, View p2, ViewGroup p3) {
			TextView tv;
			
			if(p2 == null) {
				tv = new TextView(MainActivity.this);
			} else {
				tv = (TextView) p2;
			}
			
			tv.setText(datas[p1]);
			tv.setTextSize(30);
			
			return tv;
		}
	}
}
